package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

// MR读写HBase
// 加载students表统计班级人数，将结果写回HBase
public class Demo05MRRWHBase {
    // Mapper端
    public static class HBaseMapper extends TableMapper<Text, IntWritable> {
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Mapper<ImmutableBytesWritable, Result, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // key ：rowKey value：一行数据
            String clazz = Bytes.toString(value.getValue("info".getBytes(), "clazz".getBytes()));
            context.write(new Text(clazz), new IntWritable(1));
        }
    }

    // Reducer端
    public static class HBaseReducer extends TableReducer<Text, IntWritable, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, NullWritable, Mutation>.Context context) throws IOException, InterruptedException {
            int count = 0;
            for (IntWritable value : values) {
                count += value.get();
            }
            // 输出的key不限制类型，输出的value必须是mutable实例
            // mutable有两种不同的实例：put、delete
            Put put = new Put(key.toString().getBytes());
            put.addColumn("cf1".getBytes(), "cnt".getBytes(), (count + "").getBytes());
            context.write(NullWritable.get(), put);
        }
    }

    // Driver端
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://master:9000");
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

        Job job = Job.getInstance(conf);
        job.setJarByClass(Demo05MRRWHBase.class);
        job.setJobName("Demo05MRRWHBase");
        Scan scan = new Scan(); // 如果需要进行一些过滤
        // 分别配置Map任务
        TableMapReduceUtil
                .initTableMapperJob(
                        "students"
                        , scan
                        , HBaseMapper.class
                        , Text.class
                        , IntWritable.class
                        , job
                );

        // 分别配置Reduce任务
        TableMapReduceUtil
                // 提前创建表：create 'clazz_cnt','cf1'
                .initTableReducerJob(
                        "clazz_cnt"
                        , HBaseReducer.class
                        , job
                );

        // 提交任务
        job.waitForCompletion(true);

    }
}
